package com.wxd.modules.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.annotation.DataFilter;
import com.wxd.modules.common.entity.Cause;
import com.wxd.modules.common.service.CauseService;
import com.wxd.modules.function.dao.EmpStatusChangeDao;
import com.wxd.modules.function.entity.*;
import com.wxd.modules.function.service.*;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.entity.SysUserEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.utils.Constant;
import com.wxd.utils.PageUtils;
import com.wxd.utils.Query;
import com.wxd.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("empStatusChangeService")
public class EmpStatusChangeServiceImpl extends ServiceImpl<EmpStatusChangeDao, EmpStatusChange> implements EmpStatusChangeService {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private EmpService empService;
    @Autowired
    private CauseService causeService;


    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String dateStart=(String)params.get("dateStart");
        String dateEnd=(String)params.get("dateEnd");
        String deptIdStr= (String)params.get("deptId");
        Long deptIdLong;
        //根据部门ID将部门下的所有人员查找出来
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            deptIdList.add(deptIdLong);
        }
        IPage<EmpStatusChange> page = this.page(
                new Query<EmpStatusChange>().getPage(params),
                new QueryWrapper<EmpStatusChange>().between(StringUtils.isNotBlank(dateStart),"date",dateStart,dateEnd).in(deptIdList!=null,"dept_id",deptIdList)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        for(EmpStatusChange statusChange:page.getRecords())
        {
            EmpEntity empEntity = empService.getById(statusChange.getEmpId());
            SysDeptEntity deptEntity = sysDeptService.getById(statusChange.getDeptId());
            Cause cause = causeService.queryValue(statusChange.getCause());
            statusChange.setCauseStr(cause.getCauseValue());
            statusChange.setEmpName(empEntity.getEmpName());
            statusChange.setIdCard(empEntity.getIdCard());
            statusChange.setDeptName(deptEntity.getName());
        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryExistLogPage(Map<String, Object> params) {
        String dateStart=(String)params.get("dateStart");
        String dateEnd=(String)params.get("dateEnd");
        String deptIdStr= (String)params.get("deptId");
        Long deptIdLong;
        //根据部门ID将部门下的所有人员查找出来
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            deptIdList.add(deptIdLong);
        }
        IPage<EmpStatusChange> page = this.page(
                new Query<EmpStatusChange>().getPage(params),
                new QueryWrapper<EmpStatusChange>()
                        .between(StringUtils.isNotBlank(dateStart),"date",dateStart,dateEnd)
                        .in(deptIdList!=null,"dept_id",deptIdList)
                        .eq("status",0)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        for(EmpStatusChange statusChange:page.getRecords())
        {
            EmpEntity empEntity = empService.getById(statusChange.getEmpId());
            SysDeptEntity deptEntity = sysDeptService.getById(statusChange.getDeptId());
            Cause cause = causeService.queryValue(statusChange.getCause());
            statusChange.setCauseStr(cause.getCauseValue());
            statusChange.setEmpName(empEntity.getEmpName());
            statusChange.setIdCard(empEntity.getIdCard());
            statusChange.setDeptName(deptEntity.getName());
        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryNotLogPage(Map<String, Object> params) {
        String dateStart=(String)params.get("dateStart");
        String dateEnd=(String)params.get("dateEnd");
        String deptIdStr= (String)params.get("deptId");
        Long deptIdLong;
        //根据部门ID将部门下的所有人员查找出来
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            deptIdList.add(deptIdLong);
        }
        IPage<EmpStatusChange> page = this.page(
                new Query<EmpStatusChange>().getPage(params),
                new QueryWrapper<EmpStatusChange>()
                        .between(StringUtils.isNotBlank(dateStart),"date",dateStart,dateEnd)
                        .in(deptIdList!=null,"dept_id",deptIdList)
                        .eq("status",5)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        for(EmpStatusChange statusChange:page.getRecords())
        {
            EmpEntity empEntity = empService.getById(statusChange.getEmpId());
            SysDeptEntity deptEntity = sysDeptService.getById(statusChange.getDeptId());
            Cause cause = causeService.queryValue(statusChange.getCause());
            statusChange.setCauseStr(cause.getCauseValue());
            statusChange.setEmpName(empEntity.getEmpName());
            statusChange.setIdCard(empEntity.getIdCard());
            statusChange.setDeptName(deptEntity.getName());
        }
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeStatus(ChangeEmpStatus emp) {
        Long[] empIds=emp.getEmpIds();
        Integer status=emp.getStatus();
        Integer cause=emp.getCause();
        String name=emp.getName();
        String attachment=emp.getAttachment();
        List<EmpEntity> empEntities= empService.queryEmp(empIds);
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        //校验类型，更改的信息修改
        for(EmpEntity emps:empEntities)
        {
            ValidatorUtils.validateEntity(emps);
            //记录员工在职状态变动的信息
            EmpStatusChange changeStatus=new EmpStatusChange();
            changeStatus.setEmpId(emps.getEmpId());
            changeStatus.setDeptId(emps.getDeptId());
            changeStatus.setStatus(status);
            changeStatus.setOperator(username);
            changeStatus.setDate(new Date());
            changeStatus.setCause(cause);
            changeStatus.setChangeDate(emp.getChangeDate());
            changeStatus.setName(name);
            changeStatus.setAttachment(attachment);
            this.save(changeStatus);
            emps.setStatus(status);
            empService.updateById(emps);
        }
    }

    @Override
    public List<EmpStatusChange> queryEmpStatusChange(Long empId) {
        return baseMapper.queryEmpStatusChange(empId);
    }

    @Override
    @DataFilter(subDept = true,user = false)
    public List<Map> changeExistStatusStatisticsByMonth(Map<String, Object> params) {
        return baseMapper.changeExistStatusStatisticsByMonth(params);
    }
    @Override
    @DataFilter(subDept = true,user = false)
    public List<Map> changeNotStatusStatisticsByMonth(Map<String, Object> params) {
        return baseMapper.changeNotStatusStatisticsByMonth(params);
    }

}
